<template>
  <div class="div">
    <PageTitle title="活动列表" addUrl="/RandomList/RandomDetail/none"></PageTitle>
    <el-table
      stripe
      :data="tableData"
    >
      <el-table-column
        prop="promotion_name"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="price"
        label="起始金额(元)">
      </el-table-column>
      <el-table-column
        prop="promotion_price"
        label="结束金额(元)">
      </el-table-column>
      <el-table-column
        prop="start_at"
        label="开始时间">
      </el-table-column>
      <el-table-column
        prop="end_at"
        label="结束时间">
      </el-table-column>
      <el-table-column
        label="操作"
        width="140">
      <template slot-scope="scope">
        <router-link
          :to="'/RandomList/RandomDetail/' + scope.row.id"
          class="color-theme margin-right"
        >查看</router-link>
        <router-link
          :to="'/RandomList/RandomCouponList/' + scope.row.id + '/' + scope.row.promotion_name+ '/' + scope.row.many" 
          class="color-theme margin-right"
        >查看卡券</router-link>
        <el-button type="text" @click="del(scope.row.id)">删除</el-button>
      </template>
      </el-table-column>
    </el-table>
    <div class='pagination-wrapper'>
      <el-pagination
        layout="prev, pager, next"
        @current-change="pageChange"
        :page-size="pageSize"
        :total="totalCount"
        :current-page="pageNum"
      >
      </el-pagination>
    </div>
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl">
    </el-dialog>
  </div>
</template>

<script>
  import PageTitle from '@/components/pageTitle/PageTitle.vue'
  // import _ from 'lodash'
  export default {
    name: 'MealList',
    components: {
      PageTitle
    },
    activated() {
      this._getList()
    },
    data() {
      return {
        tableData: [],
        pageNum: 1,
        totalCount: 0,
        pageSize: 10,
        status: '0',//  筛选参数
        search: '',//搜索参数
        dialogImageUrl: '',
        dialogVisible: false
      }
    },
    methods: {
      async _getList() {
        let { data: { data, code } } = await this.$axios({
          url: this.$api + '/v1/promotion/list',
          method: 'GET',
          params: {
            size: this.pageSize,
             page: this.pageNum
          }
        })
        if(code == 0) {
          this.tableData = data.items.map(item => {
            item.start_at = item.start_at.substring(0, 10)
            item.end_time = item.end_at.substring(0, 10)
            return item
          })
          this.totalCount = data.page.totalCount
        }
      },
      del(id) {
        this.$confirm('确认删除吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this._del(id)
        })
      },
      async _del(id) {
        let { data: { code, data } } = await this.$axios({
          url: this.$api + '/v1/promotion/strike',
          method: 'POST',
          data: {
            id
          }
        })
        if (code == 0) {
          this.$message.success(data)
          this._getList()
        }
      },
      changeState(id, state) {
        this.$confirm('确认变更状态吗？', '提示', {
          type: 'warning'
        }).then(() => {
          this._changeState(id, state)
        })
      },
      async _changeState (id, state) {
        let { data: { data, code } } = await this.$axios({
          url: this.$api + '/v1/goods/up',
          method: 'POST',
          data: {
            id,
            state 
          }
        })
        if(code === 0) {
          this.$message.success(data)
          this._getList()
        }
      },
      handleClick(s) {
        this.status = s
        this.pageNum = 1
        this._getList()
      },
      // handleSearch() {
      //   this.pageNum = 1
      //   this._getList()
      // },
      pageChange(pageNum) {
        this.pageNum = pageNum
        this._getList()
      },
      handlePictureCardPreview(src) {
        this.dialogImageUrl = src
        this.dialogVisible = true
      }
    }
  }
</script>

<style lang="scss" scoped>
  @import '@/assets/css/select-bar.scss';
  .list-img {
     width: 50px;
     cursor: pointer;
  }
  .el-button--text {
    text-decoration: none;
  }
</style>